#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
	int subarraySum(vector<int>& nums, int k)
	{
		unordered_map<int, int> map;
		map[0] = 1;
		int count = 0, pre = 0;
		for (auto& num : nums) {
			pre += num;
			if (map.find(pre - k) != map.end())
				count += map[pre - k];
			map[pre]++;
		}
		return count;
	}
};
